package com.linefly.online_car;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.linefly.online_car.Custom.MyLoading;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.util.HashMap;

import Util.JsonUtil;
import Util.NavigationBarUtil;
import Util.PreferenceUtil;
import okhttp3.Call;
import okhttp3.Response;


public class LoginActivity extends AppCompatActivity {

    EditText editText_shouji;
    EditText editText_yanzheng;
    TextView textView_huoqu;
    TextView textView_xiayibu;
    TextView textView_line;
    boolean is_huoqu;
    boolean is_xiayibu;
    private CountDownTimer downTimer;//倒计时
    Context context = LoginActivity.this;
    int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //AppManager.getAppManager().addActivity(Login.this);
        setContentView(R.layout.activity_login);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        }
        if (NavigationBarUtil.hasNavigationBar(this)) {
            NavigationBarUtil.initActivity(findViewById(android.R.id.content));//解决底部适配
        }

        editText_shouji = (EditText) findViewById(R.id.shouji);
        editText_yanzheng = (EditText) findViewById(R.id.yanzheng);
        textView_huoqu = (TextView) findViewById(R.id.huoqu);
        textView_xiayibu = (TextView) findViewById(R.id.xiayibu);
        textView_line = (TextView) findViewById(R.id.line);
        type = 0;

//        //倒计时设置
//        downTimer = new CountDownTimer(60 * 1000,1000) {
//            @Override
//            public void onTick(long l) {
//                textView_huoqu.setBackground(getResources().getDrawable(R.drawable.login_shape_1));
//                textView_huoqu.setText((l/1000)+"秒");
//                is_huoqu = false;
//            }
//
//            @Override
//            public void onFinish() {
//                textView_huoqu.setBackground(getResources().getDrawable(R.drawable.login_shape_2));
//                textView_huoqu.setText("获取验证码");
//                is_huoqu = true;
//            }
//        };
//
//        editText_shouji.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//                if(charSequence.length() == 11){
//                    textView_huoqu.setBackground(getResources().getDrawable(R.drawable.login_shape_2));
//                    is_huoqu = true;
//                }else {
//                    textView_huoqu.setBackground(getResources().getDrawable(R.drawable.login_shape_1));
//                    is_huoqu = false;
//                }
//
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//
//            }
//        });
//
//        editText_yanzheng.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//                if(charSequence.length() > 3 && editText_shouji.getText().toString().length() == 11){
//                    textView_xiayibu.setBackground(getResources().getDrawable(R.drawable.login_shape_2));
//                    is_xiayibu = true;
//                }else {
//                    textView_xiayibu.setBackground(getResources().getDrawable(R.drawable.login_shape_2));
//                    is_xiayibu = false;
//                }
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable editable) {
//
//            }
//        });


    }

    public void click_back(View view) {
        finish();
    }

    public void click_huoqu(View view) {
//        //获取验证码
//        if(is_huoqu){
//            //访问网络
//            //访问网络
//            MyLoading.show(context);
//            try {
//                String url = getString(R.string.url) + "/affair/sendpassword";
//
//                OkHttpUtils.post(url)
//                        .connTimeOut(10000)
//                        .params("phone",editText_shouji.getText().toString())
//                        .execute(new StringCallback() {
//                            @Override
//                            public void onSuccess(String s, okhttp3.Call call, Response response) {
//                                MyLoading.cancel();
//                                Toast.makeText(context,"获取成功",Toast.LENGTH_SHORT).show();
//                                is_xiayibu = true;
//                            }
//                            @Override
//                            public void onError(Call call, Response response, Exception e) {
//
//                                MyLoading.cancel();
//                                Toast.makeText(context, "连接服务器失败", Toast.LENGTH_LONG).show();
//                                e.printStackTrace();
//                            }
//                        });
//            } catch (Exception e) {
//                MyLoading.cancel();
//                Toast.makeText(context,"获取失败",Toast.LENGTH_SHORT).show();
//                e.printStackTrace();
//            }
//
//
//            //开启倒计时
//            downTimer.start();
//
//
//        }


        if (textView_huoqu.getText().toString().equals("IMEI号登录")) {
            textView_huoqu.setText("账号登录");
            textView_line.setVisibility(View.GONE);
            editText_shouji.setVisibility(View.GONE);
            editText_yanzheng.setHint("请输入IMEI号");
            type = 1;


        } else {
            textView_huoqu.setText("IMEI号登录");
            textView_line.setVisibility(View.VISIBLE);
            editText_shouji.setVisibility(View.VISIBLE);
            editText_yanzheng.setHint("请输入密码");
            type = 0;
        }
    }

    public void click_xiayibu(View view) {
        //点击下一步
        Boolean isLogin = false;
        if (type == 0 && editText_shouji.getText().toString().length() > 0 && editText_yanzheng.getText().toString().length() > 0) {
            isLogin = true;
        } else if (type == 1 && editText_yanzheng.length() > 0) {
            isLogin = true;
        }
        if (isLogin) {
            //访问网络
            MyLoading.show(context);
            try {
                String url = getString(R.string.url) + "/login";

                OkHttpUtils.post(url)
                        .connTimeOut(10000)
                        .params("username", editText_shouji.getText().toString())
                        .params("password", editText_yanzheng.getText().toString())
                        .params("type", type)
                        .params("imei",editText_yanzheng.getText().toString())
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, okhttp3.Call call, Response response) {
                                MyLoading.cancel();
                                Object object = null;
                                object = JsonUtil.jsonParse(s);
                                if (object instanceof HashMap) {
                                    HashMap hashMap = (HashMap) object;
                                    if (hashMap.containsKey("err")) ;
                                    int err = Integer.valueOf(hashMap.get("err").toString());
                                    if (err == 0) {
                                        //  Toast.makeText(Login.this,hashMap.get("token").toString(),Toast.LENGTH_SHORT).show();
                                        String token = hashMap.get("token").toString();
                                        PreferenceUtil.put("token", token);
                                        PreferenceUtil.put("type", type);
                                        if(type == 1){
                                            PreferenceUtil.put("imei", editText_yanzheng.getText().toString());
                                        }
                                        Log.i("token", token);
                                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                        startActivity(intent);
                                        finish();


                                        startActivity(new Intent(context, MainActivity.class));
                                    } else {
                                        Toast.makeText(context, hashMap.get("msg").toString(), Toast.LENGTH_SHORT).show();
                                    }
                                }


                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {

                                MyLoading.cancel();
                                Toast.makeText(context, "连接服务器失败", Toast.LENGTH_LONG).show();
                                e.printStackTrace();
                            }
                        });
            } catch (Exception e) {
                MyLoading.cancel();
                Toast.makeText(context, "登录失败", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
        } else {
            Toast.makeText(context, "请完善信息", Toast.LENGTH_SHORT).show();
        }
    }
}
